package com.gsbelarus.gedemin.salary.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuItem;

import com.gsbelarus.gedemin.lib.sync.protocol.SyncProtocol;
import com.gsbelarus.gedemin.lib.sync.protocol.SyncServiceModel;
import com.gsbelarus.gedemin.lib.sync.protocol.entity.SyncServiceStatus;
import com.gsbelarus.gedemin.lib.sync.protocol.entity.SyncServiceTask;
import com.gsbelarus.gedemin.lib.ui.activity.SyncActivityModel;
import com.gsbelarus.gedemin.salary.R;
import com.gsbelarus.gedemin.salary.entity.SyncRequestType;
import com.gsbelarus.gedemin.salary.fragment.payslip.PayslipFragment;
import com.gsbelarus.gedemin.salary.service.SyncService;
import com.gsbelarus.gedemin.salary.util.GCMHelper;

public class StatisticDetail extends SyncActivityModel {

    private Bundle arg;
    private PayslipFragment fragment;

    @Override
    public int getViewResource() {
        return R.layout.activity_fragment;
    }

    @Override
    public void onReceiveSyncStatus(SyncServiceStatus syncServiceStatus, SyncStatusDialog syncStatusDialog) {
        super.onReceiveSyncStatus(syncServiceStatus, syncStatusDialog);
        syncStatusDialog.show(syncServiceStatus);
        if (syncServiceStatus.getTypeOfStatus() == SyncServiceStatus.TypeOfStatus.FINISH_SYNC &&
                syncServiceStatus.getStatus() != SyncProtocol.SyncStatus.NO_INTERNET_CONNECTION)
            fragment.reloadFragmentData();
    }

    @Override
    public void onCreateActivity(Bundle arguments) {
        super.onCreateActivity(arguments);
        this.arg = arguments;
        if (getSupportActionBar() != null)
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        openFragment();
    }

    private void openFragment() {
        fragment = new PayslipFragment();
        fragment.setArguments(argumentsHelper.saveParams(arg));
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.frame_content, fragment);
        fragmentTransaction.commit();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.sync, menu);
        getMenuInflater().inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
            case R.id.action_settings:
                Intent settingIntent = new Intent(StatisticDetail.this, Settings.class);
                startActivity(settingIntent);
                return true;
            case R.id.action_update:
                startService(SyncServiceModel.getIntentWithTask(new Intent(context, SyncService.class),
                        SyncServiceTask.TypeOfTask.SINGLE_SYNC, new SyncRequestType(SyncRequestType.TypeOfRequest.DATA_MSG_REQUEST)));
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onResume() {
        super.onResume();
        new GCMHelper(this).checkPlayServices(this);
    }
}
